<?php


namespace App\Models\Admin;

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;

class Base extends Model
{
    public static function add($data)
    {
        if(!is_array($data) || empty($data)) return false;

        $res=self::create($data);
        return $res->id;
    }

    //一次插入多条
    public function addAll(Array $data)
    {
        $rs = DB::table($this->getTable())->insert($data);
        return $rs;
    }


    //无限极分类函数
    public static function generateTree($data){
        $items = $data->keyBy('id')->toArray();
        $tree = array();
        foreach($items as $k => $item){
            $items[$k]['selfNext'] = [];
            if(isset($items[$item['pid']])){
                //$item['selfNext'] = [];
                $items[$item['pid']]['selfNext'][] = &$items[$k];
            }else{
                $tree[] = &$items[$k];
            }
        }

        return $tree;
    }
    //获取菜单路径
    public static function getPathById($id)
    {
        $path_arr=array();
        do{
            $pid=self::where('id',$id)->value('pid');
            $id=$pid;
            $path_arr[]=$pid;
        }
        while($id);
        array_pop($path_arr);
        return $path_arr;
    }


}
